package com.codeoss.module.stock.service.impl;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.framework.util.DateUtil;
import com.codeoss.module.ir.service.IIrDocTypeService;
import com.codeoss.module.ir.service.IIrSequenceService;
import com.codeoss.module.stock.dao.StockOutConfirmMapper;
import com.codeoss.module.stock.model.StockOut;
import com.codeoss.module.stock.model.StockOutConfirm;
import com.codeoss.module.stock.model.StockOutConfirmLine;
import com.codeoss.module.stock.model.StockOutLine;
import com.codeoss.module.stock.service.IStockOutConfirmLineService;
import com.codeoss.module.stock.service.IStockOutConfirmService;
import com.codeoss.module.stock.service.IStockOutLineService;
import com.codeoss.module.stock.service.IStockOutService;

@Service
public class StockOutConfirmService extends BaseService<StockOutConfirmMapper, StockOutConfirm>
		implements IStockOutConfirmService ,ApplicationContextAware{
    private ApplicationContext applicationContext;
    @Autowired
	private DateUtil dateUtil;
	@Autowired
	private IIrDocTypeService irDocTypeService;
	@Autowired
	private IStockOutConfirmLineService stockOutConfirmLineService;
	@Autowired
	private IIrSequenceService irSequenceService;
	@Override
	public List<StockOutConfirm> getConfirmByStockOutId(Long stockOutId) {
		// TODO Auto-generated method stub
		QueryWrapper<StockOutConfirm> query=new QueryWrapper<StockOutConfirm>();
		query.eq("stock_out_id", stockOutId);
		return list(query);
	}

	@Override
	@Transactional
	public StockOutConfirm generateConfirm(Long stockOutId) {
		// TODO Auto-generated method stub
		QueryWrapper<StockOutConfirm> confirmQuery=new QueryWrapper<StockOutConfirm>();
		confirmQuery.eq("stock_out_id", stockOutId);
		long count=count(confirmQuery);
		if(count>0) {
			throw new RuntimeException("已经创建 不要重复创建");
		}
		IStockOutService stockOutService=applicationContext.getBean(IStockOutService.class);
		IStockOutLineService stockOutLineService=applicationContext.getBean(IStockOutLineService.class);
		StockOut stockOut=stockOutService.getById(stockOutId);
		String docStatus=stockOut.getDocStatus();
		if(!"DR".equals(docStatus)) {
			throw new RuntimeException("只有草稿状态 才能创建");
		}
		StockOutConfirm stockOutConfirm=createFromStockOut(stockOut);
		List<StockOutLine> stockOutLineList=stockOutLineService.getLineListByStockOutId(stockOutId);
		for(int i=0;i<stockOutLineList.size();i++) {
			StockOutLine stockOutLine=stockOutLineList.get(i);
			stockOutConfirmLineService.createConfirmLine(stockOutConfirm, stockOutLine);
		}
		return stockOutConfirm;
	}

	@Transactional
	protected StockOutConfirm createFromStockOut(StockOut stockOut) {
		StockOutConfirm stockOutConfirm=new StockOutConfirm();
		stockOutConfirm.setStockOutId(stockOut.getId());
		stockOutConfirm.setDocStatus("DR");
		Long docTypeId=irDocTypeService.getIrDocTypeIdByCode("stockoutconfirm");
		stockOutConfirm.setIrDocTypeId(docTypeId);
		stockOutConfirm.setDateConfirmed(dateUtil.getSqlNowDatetime());
		stockOutConfirm.setDocumentNo(irSequenceService.getNextSequenceNo("stockoutconfirm"));
		insert(stockOutConfirm);
		return stockOutConfirm;
	}
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext=applicationContext;
	}

	@Override
	@Transactional
	public void prepareIt(Long id) {
		// TODO Auto-generated method stub
		List<StockOutConfirmLine> lineList=stockOutConfirmLineService.getLineListByStockOutConfirmId(id);
		if(lineList==null ||lineList.size()==0) {
			throw new RuntimeException("没有明细");
		}
	}

	@Override
	@Transactional
	public void completeIt(Long id) {
		// TODO Auto-generated method stub
		StockOutConfirm stockOutConfirm=getById(id);
		stockOutConfirm.setDocStatus("CO");
		update(stockOutConfirm);
		if(!stockOutConfirm.getApproved()) {
			return;
		}
		IStockOutLineService stockOutLineService=applicationContext.getBean(IStockOutLineService.class);
		List<StockOutConfirmLine> confirmLineList=stockOutConfirmLineService.getLineListByStockOutConfirmId(id);
		for(int i=0;i<confirmLineList.size();i++) {
			StockOutConfirmLine confirmLine=confirmLineList.get(i);
			Long stockOutLineId=confirmLine.getStockOutLineId();
			StockOutLine stockOutLine=stockOutLineService.getById(stockOutLineId);
			stockOutLine.setQtyEntered(confirmLine.getConfirmedQty());
			stockOutLineService.update(stockOutLine);
		}
	}

	@Override
	@Transactional
	public void approveIt(Long id) {
		// TODO Auto-generated method stub
		StockOutConfirm stockOutConfirm=getById(id);
		stockOutConfirm.setApproved(true);
		update(stockOutConfirm);
	}

	@Override
	@Transactional
	public void rejectIt(Long id) {
		// TODO Auto-generated method stub
		StockOutConfirm stockOutConfirm=getById(id);
		stockOutConfirm.setApproved(false);
		update(stockOutConfirm);
	}

	@Override
	@Transactional
	public void setDocStatus(Long id, String docStatus) {
		// TODO Auto-generated method stub
		StockOutConfirm stockOutConfirm=getById(id);
		stockOutConfirm.setDocStatus(docStatus);
		update(stockOutConfirm);
	}
}